# coding:utf-8
# @Time : 2021/5/13 15:47 
# @Author : minuy
# @File : order.py
from flask import request

from app import login_auth, db
from app.api import api
from app.api.interface import api_result
from app.models.message import Message
from app.models.okami import Okami
from app.models.order import Order


@api.route('/order/creat', methods=['POST'])
@login_auth.login_required
def create_order():
    user = login_auth.current_user
    # 兼容提交
    json_data = request.get_json()
    args_data = request.args
    if json_data:
        post_form = json_data
    else:
        post_form = args_data
    print(post_form)

    if post_form:
        order = post_form.get('order')
        if isinstance(order, dict):
            print('是dict', order)
            ok_id = order.get('ok_id')
            number = order.get('number')
            remarks = order.get('remarks')

            if ok_id and number:
                try:
                    number = int(number)
                except Exception as e:
                    print('价格格式错误')
                    return api_result(code=403, message='价格格式错误')
                if not (100 >= len(str(remarks)) >= 0):
                    print('备注格式错误')
                    return api_result(code=403, message='备注格式错误')

                print('参数完整')
                if user.coin >= 0:
                    okami_info = Okami.query.filter_by(id=ok_id).first()
                    if okami_info:
                        print('存在发布', okami_info)
                        if okami_info.user_id == user.id:
                            return api_result(code=403, message='不可以给自己下单')

                        total = okami_info.price * number

                        order = Order(user_id=user.id,
                                      server_id=okami_info.user_id,
                                      okami_id=ok_id,
                                      number=number,
                                      remarks=remarks,
                                      total=total)

                        # 消息提醒
                        msg_self = Message(user_id=user.id, message='下单成功')
                        msg_server = Message(user_id=okami_info.user_id, message='有人下单了')

                        try:
                            db.session.add(order)
                            db.session.add(msg_self)
                            db.session.add(msg_server)
                            db.session.commit()
                        except Exception as e:
                            print('写入数据库时错误', e)
                            return api_result(code=500, message='写入数据库时出错')

                        return api_result(code=200, data={
                            'order_id': order.id
                        })
                else:
                    print('余额不足', user.coin)
                    return api_result(code=403, message='余额不足')
    return api_result(code=403, message='参数不完整')


@api.route('/order/query')
@login_auth.login_required
def get_order():
    user = login_auth.current_user
    # 兼容提交
    json_data = request.get_json()
    args_data = request.args
    if json_data:
        post_form = json_data
    else:
        post_form = args_data

    if post_form:
        print(post_form)
        orderList: [{
            time: '2021-5-2 12:45',
            stateTipColor: 'red',
            stateTip: '已取消',
            state: 2,
            goodsList: [{
                image: '',
                title: '和平精英',
                attr: '7.4',
                number: '2',
                price: 14.9,
            }]
        }]
    return api_result(code=403,message='参数不完整')
